
var XhrObj = new XMLHttpRequest();

var url = "https://localhost/planmytrip/logon_provider_db.php";

var logon_provider_callback = function() {
    if (XhrObj.readyState === 4) {/* request complete */
        if (XhrObj.status === 200) { /* HTTP return code 200 : OK */
            var res = this.responseText;
            if (res === "Bad password") {
                document.getElementById("logon_provider_passwd").value = '';
                document.getElementById("logon_provider_status").innerHTML = res;
            } else if (res === "OK") {
                location.href = "provider.php";
            } else if (res === "XSRF") {
                alert('XSRF eploit detected. You will be redirected to the start page.');
                location.href = "index.php";
            } else if (res === "timeout") {
                alert('Timeout. You will be redirected to the start page.');
                location.href = "index.php";
            }
        }
    }
};

var logon_provider = function() {
    var login = document.getElementById("logon_provider_login").value,
            passwd = document.getElementById("logon_provider_passwd").value;
    if (login !== "" && passwd !== "") {
        XhrObj.open("POST", url, true);
        XhrObj.onreadystatechange = logon_provider_callback;
        XhrObj.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        XhrObj.send("login=" + login + "&passwd=" + passwd + "&token=" + token1);
    } else {
        alert('Please fill all the fields of the form.');
    }
};

var logon_provider_enter = function(event) {
    var login = document.getElementById("logon_provider_login").value,
            passwd = document.getElementById("logon_provider_passwd").value;
    if (event && event.keyCode === 13) {
        if (login !== "" && passwd !== "") {
            XhrObj.open("POST", url, true);
            XhrObj.onreadystatechange = logon_provider_callback;
            XhrObj.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
            XhrObj.send("login=" + login + "&passwd=" + passwd + "&token=" + token1);
        } else {
            alert('Please fill all the fields of the form.');
        }
    }
};
var XhrObj2 = new XMLHttpRequest();

var url2 = "http://localhost/planmytrip/create_provider_account_db.php";

var register_provider_callback = function() {
    if (XhrObj2.readyState === 4) {
        if (XhrObj2.status === 200) {
            var res = this.responseText;
            if (res === "OK") {
                location.href = "provider.php";
            } else if (res === "XSRF") {
                alert('XSRF eploit detected. You will be redirected to the start page.');
                location.href = "index.php";
            } else if (res === "timeout") {
                alert('Timeout. You will be redirected to the start page.');
                location.href = "index.php";
            }
        }
    }
};

var register_provider = function() {
    var name = document.getElementById('new_provider_name').value,
            login = document.getElementById('new_provider_login').value,
            passwd = document.getElementById('new_provider_passwd').value,
            retyped = document.getElementById('new_provider_retyped').value;
    if (name !== '' && login !== '' && passwd !== '' && retyped !== '') {
        if (passwd === retyped) {
            XhrObj2.open("POST", url2, true);
            XhrObj2.onreadystatechange = register_provider_callback;
            XhrObj2.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
            XhrObj2.send("name=" + name + "&login=" + login + "&passwd=" + passwd + "&token=" + token2);
        } else {
            passwd = '';
            retyped = '';
            alert('Please retype password carefully.');
        }
    } else {
        alert('Please fill all the fields of the form.');
    }
};

var XhrObj3 = new XMLHttpRequest();

var url3 = "http://localhost/planmytrip/add_service_db.php";

var add_service_callback = function() {
    if (XhrObj3.readyState === 4) {
        if (XhrObj3.status === 200) {
            var res = this.responseText;
            if (res === "OK") {
                alert('Service added succesfully');
                location.reload();
            } else if (res === "XSRF") {
                alert('XSRF eploit detected. You will be redirected to the start page.');
                location.href = "index.php";
            } else if (res === "timeout") {
                alert('Timeout. You will be redirected to the logon page.');
                location.href = "logon_provider.php";
            }
        }
    }
};

var add_service = function() {
    var descr = document.getElementById('service_descr').value,
            price = document.getElementById('service_price').value,
            location = document.getElementById('service_location').value,
            type = '';
    if (document.add_service_form.service_type[0].checked) {
        type = document.add_service_form.service_type[0].value;
    } else if (document.add_service_form.service_type[1].checked) {
        type = document.add_service_form.service_type[1].value;
    } else if (document.add_service_form.service_type[2].checked) {
        type = document.add_service_form.service_type[2].value;
    }
    if (descr !== '' && price !== '' && type !== '' && location !== '') {
        XhrObj3.open("POST", url3, true);
        XhrObj3.onreadystatechange = add_service_callback;
        XhrObj3.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        XhrObj3.send("type=" + type + "&location="+ location +"&descr=" + descr + "&price=" + price + "&token=" + token3);
    } else {
        alert('Please check a type of service and fill all the fields of the form.');
    }
};
